package com.ktg.mes.rec.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktg.common.core.domain.APIResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.core.domain.Result;
import com.ktg.mes.rec.domain.RecTestingRecord;
import com.ktg.mes.rec.service.IRecTestingRecordService;
import com.ktg.common.utils.poi.ExcelUtil;

/**
 * 检测记录Controller
 *
 * @author byteco
 * @date 2022-10-31
 */
@RestController
@RequestMapping("/rec/recTestingRecord")
public class RecTestingRecordController extends BaseController
{
    @Autowired
    private IRecTestingRecordService recTestingRecordService;

    /**
     * 选择数据记录具体桶
     */
    @GetMapping("/record")
    public Result getRecord(@RequestParam(name = "type") String type,HttpServletRequest req){
        APIResult apiLog = new APIResult("查询分散or加粉or研磨-记录列表",req,null);
        List list = recTestingRecordService.selectRecord(type);
        return apiLog.ok(list);
    }

    /**
     * 查询检测记录列表
     */
    // @PreAuthorize("@ss.hasPermi('rec:recTestingRecord:list')")
    @GetMapping("/list")
    public Result<IPage<RecTestingRecord>> list(RecTestingRecord recTestingRecord,
                                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("查询检测记录列表",req,null);
        try {
            Page<RecTestingRecord> page = new Page<RecTestingRecord>(pageNo, pageSize);
            IPage<RecTestingRecord> pageList = recTestingRecordService.queryPageList(recTestingRecord,page);
            return apiLog.ok(pageList);
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 导出检测记录列表
     */
    // @PreAuthorize("@ss.hasPermi('rec:recTestingRecord:export')")
    @Log(title = "检测记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecTestingRecord recTestingRecord,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("导出检测记录列表",req,recTestingRecord);
        try {
            List<RecTestingRecord> list = recTestingRecordService.queryList(recTestingRecord);
            ExcelUtil<RecTestingRecord> util = new ExcelUtil<RecTestingRecord>(RecTestingRecord.class);
            util.exportExcel(response, list, "检测记录数据");
            apiLog.ok(null);
        }catch (Exception e){
            apiLog.error(e);
        }
    }

    /**
     * 获取检测记录详细信息
     */
    // @PreAuthorize("@ss.hasPermi('rec:recTestingRecord:query')")
    @GetMapping(value = "/{testingId}")
    public Result<RecTestingRecord> getInfo(@PathVariable("testingId") Long testingId,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("获取检测记录详细信息",req,null);
        try {
            return apiLog.ok(recTestingRecordService.getInfo(testingId));
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 新增检测记录
     */
    // @PreAuthorize("@ss.hasPermi('rec:recTestingRecord:add')")
    @Log(title = "检测记录", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<String> add(@RequestBody RecTestingRecord recTestingRecord,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("新增检测记录",req,recTestingRecord);
        try {
            recTestingRecordService.insert(recTestingRecord);
            return apiLog.ok("添加成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 修改检测记录
     */
    // @PreAuthorize("@ss.hasPermi('rec:recTestingRecord:edit')")
    @Log(title = "检测记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<String> edit(@RequestBody RecTestingRecord recTestingRecord,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("修改检测记录",req,recTestingRecord);
        try {
            recTestingRecordService.update(recTestingRecord);
            return apiLog.ok("编辑成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 删除检测记录
     */
    // @PreAuthorize("@ss.hasPermi('rec:recTestingRecord:remove')")
    @Log(title = "检测记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{testingIds}")
    public Result<String> remove(@PathVariable Long[] testingIds,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("删除检测记录",req,null);
        try {
            recTestingRecordService.delete(testingIds);
            return apiLog.ok("删除成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }

    }
}
